
-- "These gateways are imperfect.. and with each crossing I can feel my own imperfections mounting."
-- "My form is unraveling."


Level.CurCamPos = { };
Level.FadeAlpha = 255;

Level.FadingIn = false;
Level.FadingOut = false;

local function FadeIn()
	
	Level.FadingIn = true;
	Level.FadingOut = false;
	
end

local function FadeOut()
	
	Level.FadingIn = false;
	Level.FadingOut = true;
	
end

local function FadeThink()
	
	if( Level.FadingIn ) then
		
		Level.FadeAlpha = Level.FadeAlpha - 100 * GetDPS();
		
		if( Level.FadeAlpha < 0 ) then
			
			Level.FadeAlpha = 0;
			Level.FadingIn = false;
			
		end
		
	end
	
	
	if( Level.FadingOut ) then
		
		Level.FadeAlpha = Level.FadeAlpha + 100 * GetDPS();
		
		if( Level.FadeAlpha > 255 ) then
			
			Level.FadeAlpha = 255;
			Level.FadingOut = false;
			
		end
		
	end
	
end

local IntroText = { };

local function FlashText( text, length )
	
	table.insert( IntroText, {
		
		Text = text,
		End = GetTicks() + length,
		Width = GetStringWidth( "Uni20Bold", text ),
		
	} );
	
end

local function PlaceCamera( x, y )
	
	Level.CurCamPos = { x = x, y = y };
	
	SetCameraPos( Level.CurCamPos.x, Level.CurCamPos.y );
	
end

function Level.Loaded()

	LockCamera();
	PlaceCamera( 200, -10 );
	
	Level.StartTime = GetTicks();
	
	Level.HueyLevel = 0;
	
	Level.CamVel = 20;
	
	timer.Simple( 2000, function()
	Level.HueyLevel = 1;
	
	timer.Simple( 3000, FadeIn );
	timer.Simple( 6000, function() FlashText( "Rick Chou presents", 4000 ); end );
	timer.Simple( 7100, FadeOut );
	timer.Simple( 11000, function() Level.HueyLevel = 2; PlaceCamera( 1400, -10 ); FadeIn(); end );
	timer.Simple( 15000, function() FlashText( "A Rick Dark production", 6000 ); end );
	timer.Simple( 17000, FadeOut );
	timer.Simple( 21000, function() Level.HueyLevel = 3; PlaceCamera( 1400, -10 ); FadeIn(); end );
	timer.Simple( 25000, function() FlashText( "Developed by Dick Rark", 6000 ); end );
	timer.Simple( 27000, FadeOut );
	timer.Simple( 32000, function() Level.HueyLevel = 4; PlaceCamera( 2600, -10 ); FadeIn(); end );
	timer.Simple( 36500, FadeOut );
	timer.Simple( 40000, function() 
	
		Level.HueyLevel = 5; 
		PlaceCamera( 4080, -10 ); 
		Weather.GenerateFullSnow(); 
		
		local p = GetPlayer();
		
		p:SetPos( 4500, 340 );
		p:SetCurrentSprite( 4 );
		p:Flip();
		p:SetConstantVelocity( 105, 0 );
		
		FadeIn(); 

	end );
	timer.Simple( 45000, function() FlashText( "Huey Doesn't Change The World", 8000 ); end );
	timer.Simple( 47000, FadeOut );
	timer.Simple( 54000, function() UnlockCamera(); LoadMap( "maps/map1.map" ); end );
	
end );
	
end

function Level.PreMapDraw()

	Draw.SetColor( 1, 1, 1, 1 );
	
	if( Level.HueyLevel == 1 ) then
		Draw.Texture( "Graphics/Backgrounds/HueyPast/forestmountain.png", 0, -200, 1200, 900 );
	elseif( Level.HueyLevel == 2 ) then
		Draw.Texture( "Graphics/Backgrounds/HueyPast/intro.png", 1200, -200, 1600, 700 );
	elseif( Level.HueyLevel == 4 ) then
		Draw.Texture( "Graphics/Backgrounds/HueyPast/snow.png", 2500, -200, 1200, 700 );
	elseif( Level.HueyLevel == 5 ) then
		
		Draw.SetColor255( 50, 50, 90, 255 );
		Draw.Rect( 4000, 0, 1500, 600 );
		
	end
	
end

function Level.Think()
	
	
	if( Level.HueyLevel == 0 ) then return; end
	
	if( Level.HueyLevel ~= 3 ) then
		Level.CurCamPos.x = Level.CurCamPos.x + Level.CamVel * GetDPS();
		
		if( Level.HueyLevel == 5 ) then
			
			Level.CamVel = Level.CamVel - 4 * GetDPS();
			
			if( Level.CamVel < 0 ) then
				
				Level.CamVel = 0;
				
			end
			
		end
		
	else
		Level.CurCamPos.y = Level.CurCamPos.y + 15 * GetDPS();
	end
	
	if( Level.HueyLevel == 2 ) then
		Level.CurCamPos.y = Level.CurCamPos.y - 10 * GetDPS();
	end
	
	SetCameraPos( Level.CurCamPos.x, Level.CurCamPos.y );
	
end

function Level.PostDraw()

	if( Level.HueyLevel == 3 ) then
		Draw.Texture( "Graphics/Backgrounds/HueyPast/darkstar.png", 1200 - Level.CurCamPos.x, -300 - Level.CurCamPos.y, 1000, 1200 );
		
		Draw.Texture( "Graphics/Misc/HueyPast/bwprismcolorglow.png", 1748 - Level.CurCamPos.x, 450 - Level.CurCamPos.y, 125, 180 );
		Draw.Texture( "Graphics/Misc/HueyPast/bwprism.png", 1755 - Level.CurCamPos.x, 460 - Level.CurCamPos.y, 110, 160 );
		
	end
	
	if( Level.HueyLevel == 5 ) then
	
		Draw.SetColor( 0, 0, 0, .3 );
		Draw.Rect( 0, 0, 800, 600 );
		
		Weather.DoSnow();
	
	end
		
	Draw.SetColor( 0, 0, 0, 1 );
	Draw.Rect( 0, 0, 800, 100 );
	Draw.Rect( 0, 500, 800, 100 );
	
	Draw.SetColor255( 0, 0, 0, Level.FadeAlpha );
	Draw.Rect( 0, 0, 800, 600 );
	
	for k, v in pairs( IntroText ) do
		
		local text = v.Text;
		local endtime = v.End;
		local width = v.Width;
		
		if( GetTicks() > endtime ) then
			
			IntroText[k] = nil;
			
		else
			
			Draw.SetColor( 1, 1, 1, 1 );
			Draw.Text( "Uni20Bold", text, 400 - width * .5, 295 );
			
		end
		
	end
	
	FadeThink();
	
end